package com.shushan.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.db.ChildBookDao;
import com.shushan.home.adapter.AbsCommonAdapter;
import com.shushan.login.bean.ChildBookBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.setting.bean.BookBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;
import com.shushan.util.Session;
import com.shushan.versionupdate.utils.MD5Util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * @ClassName: PressSelectActivity
 * @Desciption: 版本选择
 * @author: yichaohua
 * @date: 2018-08-09
 */
public class PressSelectActivity extends NavigationLoseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private static final String TAG = "PressSelectActivity";

    private ListView pressListView;

    private List<BookBean> bookList;
    private AbsCommonAdapter bookAdapter;
    private Button confirmButton;

    private BookBean selectBookBean;
    private boolean isParentSet;
    private BabyApplication babyApplication = BabyApplication.getInstance();
    private int gradeId = 1;
    private String semester = "UP";
    private BookBean chineseBook;
    private BookBean mathBook;
    private BookBean englishBook;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.press_select_activity);

        setTitle("版本选择");

        isParentSet = getIntent().getBooleanExtra("isParentSet",false);
        gradeId = getIntent().getIntExtra("gradeId",1);
        semester = getIntent().getStringExtra("semester");
        bookList = (List<BookBean>) Session.getSession().get("bookList");
        chineseBook = (BookBean) Session.getSession().get("chineseBook");
        mathBook = (BookBean) Session.getSession().get("mathBook");
        englishBook = (BookBean) Session.getSession().get("englishBook");

        initView();
    }

    private void initView() {
        pressListView = findViewById(R.id.press_select_lv);
        pressListView.setOnItemClickListener(this);
        confirmButton = findViewById(R.id.press_select_btn);
        confirmButton.setOnClickListener(this);

        bookAdapter = new AbsCommonAdapter<BookBean>(this,R.layout.press_select_list_item,bookList) {
            @Override
            public void convert(ViewHolder helper, BookBean item, int selectedItem) {
                if(selectBookBean != null && selectBookBean.id == item.id){
                    helper.setImageResource(R.id.press_select_item_selected_img,R.drawable.rd_checked);
                }else {
                    helper.setImageResource(R.id.press_select_item_selected_img,R.drawable.rd_normal);
                }

                ImageUtils.loadImageNoBg(PressSelectActivity.this,item.cover,
                        (ImageView) helper.getView(R.id.press_select_item_press_img));

                helper.setText(R.id.press_select_item_press_name_tv,item.press_name);
                helper.setText(R.id.press_select_item_press_desc_tv,item.press_desc);
            }
        };
        pressListView.setAdapter(bookAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectBookBean = bookList.get(position);
        if(selectBookBean.subject_id == Constants.SUBJECT_CHINESE_ID){
            chineseBook = selectBookBean;
        }else if(selectBookBean.subject_id == Constants.SUBJECT_MACH_ID){
            mathBook = selectBookBean;
        }else if(selectBookBean.subject_id == Constants.SUBJECT_ENGLISH_ID){
            englishBook = selectBookBean;
        }

        bookAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.press_select_btn:
                if(selectBookBean != null){
                    if(isParentSet){
                        submitBooks();
                    }else {
                        Session.getSession().put("selectBookBean",selectBookBean);
                        onBackAction(100);
                    }

                }else {
                    showToast("请选择版本");
                }
                break;
            default:
                break;
        }
    }

    private void submitBooks() {
        showWaitDialog();

        int childId = babyApplication.getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.POST_CHILD_BOOKS);
        urlParse.putValue("child_id", childId)
                .putValue("grade_id", gradeId)
                .putValue("semester", semester)
                .putValue("outline_info", getOutline())
                .putValue("sign", MD5Util.MD5LOW(childId + "sszx" + gradeId + semester));

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        setBookDao();

                        Session.getSession().put("selectBookBean",selectBookBean);
                        onBackAction(100);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    private String getOutline() {
        JSONArray jsonArray = new JSONArray();
        if(chineseBook != null){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("id",chineseBook.id);
                jsonObject.put("level",chineseBook.level);
                jsonObject.put("subject_id",chineseBook.subject_id);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            jsonArray.put(jsonObject);
        }

        if(mathBook != null){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("id",mathBook.id);
                jsonObject.put("level",mathBook.level);
                jsonObject.put("subject_id",mathBook.subject_id);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            jsonArray.put(jsonObject);
        }

        if(englishBook != null){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("id",englishBook.id);
                jsonObject.put("level",englishBook.level);
                jsonObject.put("subject_id",englishBook.subject_id);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            jsonArray.put(jsonObject);
        }

        return jsonArray.toString();
    }

    private void setBookDao() {
        ChildBookDao childBookDao = new ChildBookDao(this);
        if(chineseBook != null || mathBook != null || englishBook != null){
            childBookDao.clearTab();
        }

        if(chineseBook != null){
            ChildBookBean childBookBean = new ChildBookBean();
            childBookBean.id = chineseBook.id;
            childBookBean.grade_id = chineseBook.grade_id;
            childBookBean.grade_name = chineseBook.grade_name;
            childBookBean.semester = chineseBook.semester;
            childBookBean.subject_id = chineseBook.subject_id;
            childBookBean.subject_name = chineseBook.subject_name;
            childBookBean.press_id = chineseBook.press_id;
            childBookBean.press_name = chineseBook.press_name;
            childBookBean.cover = chineseBook.cover;

            childBookDao.add(childBookBean);
        }

        if(mathBook != null){
            ChildBookBean childBookBean = new ChildBookBean();
            childBookBean.id = mathBook.id;
            childBookBean.grade_id = mathBook.grade_id;
            childBookBean.grade_name = mathBook.grade_name;
            childBookBean.semester = mathBook.semester;
            childBookBean.subject_id = mathBook.subject_id;
            childBookBean.subject_name = mathBook.subject_name;
            childBookBean.press_id = mathBook.press_id;
            childBookBean.press_name = mathBook.press_name;
            childBookBean.cover = mathBook.cover;

            childBookDao.add(childBookBean);
        }

        if(englishBook != null){
            ChildBookBean childBookBean = new ChildBookBean();
            childBookBean.id = englishBook.id;
            childBookBean.grade_id = englishBook.grade_id;
            childBookBean.grade_name = englishBook.grade_name;
            childBookBean.semester = englishBook.semester;
            childBookBean.subject_id = englishBook.subject_id;
            childBookBean.subject_name = englishBook.subject_name;
            childBookBean.press_id = englishBook.press_id;
            childBookBean.press_name = englishBook.press_name;
            childBookBean.cover = englishBook.cover;

            childBookDao.add(childBookBean);
        }
    }
}
